/**
 * @author Ahammer
 * @date   2009 09
 * @update 2010 01 output image type option
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tools;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ExtractSVSSelection implements ActionListener, ChangeListener {
	private JSpinner jSpinnerImgNum;
	private JSpinner jSpinnerRescale;
	private int maxImgNum = 1;
	private int imgNum  = 1;   //image number 
	private int rescale = 1;  // rescaling factor  formula: original size/rescale
	private int interP  =1;
	private int extension = 0; //tif output image
	
	private JRadioButtonMenuItem  buttNN         = null;	//nearest neighbor
	private JRadioButtonMenuItem  buttBL         = null;	//bilinear
	private JRadioButtonMenuItem  buttBC         = null;	//bicubic
	private JRadioButtonMenuItem  buttBC2        = null;	//bicubic2
	private JRadioButtonMenuItem  buttIntPHidden = null;
	private JToolBar              jToolBarIntP   = null;
	private ButtonGroup           buttGroupIntP  = null;
	
	private JRadioButtonMenuItem  buttTif       = null;	// output image
	private JRadioButtonMenuItem  buttJpg       = null;	//
	private JRadioButtonMenuItem  buttPng       = null;	//
	private JRadioButtonMenuItem  buttBmp       = null;	//
	private JRadioButtonMenuItem  buttExtHidden = null;
	private JToolBar              jToolBarExt   = null;
	private ButtonGroup           buttGroupExt  = null;


	public ExtractSVSSelection(int maxImgNum){
		  this.maxImgNum = maxImgNum;
	}

	  public void displayDialog() {
		    JFrame frame = new JFrame();

		    JOptionPane optionPane = new JOptionPane();
		    optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
		    //optionPane.setOptionType(JOptionPane.OK_OPTION);
		    jSpinnerImgNum  = getJSpinnerImgNum();
		    jSpinnerRescale = getJSpinnerRescale();
		    JToolBar jToolBarInterP  = getJToolBarIntP();
		    JToolBar jToolBarExt  = getJToolBarExt();
		    buttBL.setSelected(true);
		    buttTif.setSelected(true);
		    optionPane.setMessage(new Object[] {"Image Number: ",       jSpinnerImgNum,
		    									"Rescale Factor:",      jSpinnerRescale,
		    									"Interpolation method", jToolBarInterP,
		    									"Image extension",      jToolBarExt,
		    									});
		    frame.pack();
		    JDialog dialog = optionPane.createDialog(frame, "Choose Values");
		    dialog.setVisible(true);
		  
	  }
	  public int[]getSelectedValues() {
		    System.out.println("Selected Image Number : "   + imgNum);
		    System.out.println("Selected Rescale Factor : " + rescale);
		    System.out.println("Selected Interpolation Method: " + interP);
		    System.out.println("File extension: " + extension);
			return new int[]{imgNum, rescale, interP, extension}; 
	  }

	  
	 /**
	  * This method returns a spinner for the image number selection
	  * @return
	  */
	 private JSpinner getJSpinnerImgNum() {
	        JFormattedTextField ftf = null;
	 		SpinnerModel sModel = new SpinnerNumberModel(1, 1, this.maxImgNum, 1); //init, min, max, step 
	        JSpinner jSpinnerImgNum = new JSpinner(sModel);
	        //Set the formatted text field.
	        ftf = getTextField(jSpinnerImgNum);
	        if (ftf != null ) {
	            ftf.setColumns(5); 
	            ftf.setHorizontalAlignment(JTextField.CENTER);	
	            ftf.setPreferredSize(new Dimension(20,20));
	            ftf.setEditable(false);
	        }
//	        //add ChangeListener
//		    ChangeListener changeListener = new ChangeListener() {
//		      public void stateChanged(ChangeEvent changeEvent) {
//		        JSpinner theSpinner = (JSpinner) changeEvent.getSource();
//		        //if (!theSpinner.getValueIsAdjusting()) {
//		          imgNum = (((Number)theSpinner.getValue()).intValue());
//		        //}
//		      }
//		    };
		    jSpinnerImgNum.addChangeListener(this);
		    return jSpinnerImgNum;
	}
	 /**
	  * This method returns a spinner for the rescale factor selection
	  * @return
	  */
	 private JSpinner getJSpinnerRescale() {
	        JFormattedTextField ftf = null;
	 		SpinnerModel sModel = new SpinnerNumberModel(1, 1, 10 , 1); //init, min, max, step 
	        JSpinner jSpinnerRescale = new JSpinner(sModel);
	        //Set the formatted text field.
	        ftf = getTextField(jSpinnerRescale);
	        if (ftf != null ) {
	            ftf.setColumns(5); 
	            ftf.setHorizontalAlignment(JTextField.CENTER);	
	            ftf.setPreferredSize(new Dimension(20,20));
	            ftf.setEditable(false);
	        }
//	        //add ChangeListener
//		    ChangeListener changeListener = new ChangeListener() {
//		      public void stateChanged(ChangeEvent changeEvent) {
//		        JSpinner theSpinner = (JSpinner) changeEvent.getSource();
//		        //if (!theSpinner.getValueIsAdjusting()) {
//		          rescale = (((Number)theSpinner.getValue()).intValue());
//		        //}
//		      }
//		    };
		    jSpinnerRescale.addChangeListener(this);
		    return jSpinnerRescale;
	}
	  	/**
	     * Return the formatted text field used by the editor, or
	     * null if the editor doesn't descend from JSpinner.DefaultEditor.
	     */
	    public JFormattedTextField getTextField(JSpinner spinner) {
	        JComponent editor = spinner.getEditor();
	        if (editor instanceof JSpinner.DefaultEditor) {
	            return ((JSpinner.DefaultEditor)editor).getTextField();
	        } else {
	            System.err.println("Unexpected editor type: "
	                               + spinner.getEditor().getClass()
	                               + " isn't a descendant of DefaultEditor");
	            return null;
	        }
	    }
		/**
		 * This method initializes the Option: Nearest Neighbor	
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtNN() {
			//if (buttNN == null) {
				buttNN = new JRadioButtonMenuItem();
				buttNN.setText("Nearest Neighbor");
				buttNN.setToolTipText("uses Nearest Neighbor resampling for resizing");
				buttNN.addActionListener(this);
				buttNN.setActionCommand("parameter");
			//}
			return buttNN;
		}
		/**
		 * This method initializes the Option: Bilinear interpolation
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtBL() {
			//if (buttBL == null) {
				buttBL = new JRadioButtonMenuItem();
				buttBL.setText("Bilinear");
				buttBL.setToolTipText("uses Bilinear interpolation for resizing");
				buttBL.addActionListener(this);
				buttBL.setActionCommand("parameter");
			//}
			return buttBL;
		}
		/**
		 * This method initializes the Option: Bicubic interpolation	
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtBC() {
			//if (buttBC == null) {
				buttBC = new JRadioButtonMenuItem();
				buttBC.setText("Bicubic");
				buttBC.setToolTipText("uses Bicubic interpoolation for resizing");
				buttBC.addActionListener(this);
				buttBC.setActionCommand("parameter");
			//}
			return buttBC;
		}
		/**
		 * This method initializes the Option: Bicubic2 interpolation	
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtBC2() {
			//if (buttBC2 == null) {
				buttBC2 = new JRadioButtonMenuItem();
				buttBC2.setText("Bicubic2");
				buttBC2.setToolTipText("uses Bicubic2 interpolation for resizing");
				buttBC2.addActionListener(this);
				buttBC2.setActionCommand("parameter");
			//}
			return buttBC2;
		}
		
		/**
		 * This method initializes the hidden option	
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtIntPHidden() {
			//if (buttIntPHidden == null) {
				buttIntPHidden  = new JRadioButtonMenuItem();
				buttIntPHidden .setText("Hidden");
				buttIntPHidden.setVisible(false);
			//}
			return buttIntPHidden;
		}


		/**
		 * This method initializes jJToolBarBar	
		 * 	
		 * @return javax.swing.JToolBar	
		 */
		private   JToolBar getJToolBarIntP() {
//			if (jToolBarIntP== null) {
				jToolBarIntP = new JToolBar("Options");
				jToolBarIntP.setOrientation(1); // 0 Horizontal 1 Vertical orientation
				jToolBarIntP.add(getJRadioButtonMenuButtNN());
				jToolBarIntP.add(getJRadioButtonMenuButtBL());
				jToolBarIntP.add(getJRadioButtonMenuButtBC());
				jToolBarIntP.add(getJRadioButtonMenuButtBC2());
				jToolBarIntP.add(getJRadioButtonMenuButtIntPHidden());
				//jToolBarIntP.addSeparator();
				this.setButtonGroupIntP();	//Gruppieren der JRadioButtons
//			}
			return jToolBarIntP;
		}

		private  void setButtonGroupIntP() {
			//if (ButtonGroup buttGroup == null) {
				buttGroupIntP = new ButtonGroup();
				buttGroupIntP.add(buttNN);
				buttGroupIntP.add(buttBL);
				buttGroupIntP.add(buttBC);
				buttGroupIntP.add(buttBC2);
				buttGroupIntP.add(buttIntPHidden);
		}
		//------------------------------------------------------------------------------------------
		/**
		 * This method initializes the Option:
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtTif() {
			//if (buttTif == null) {
				buttTif  = new JRadioButtonMenuItem();
				buttTif.setText("Tif");
				buttTif.setToolTipText("Tif output image");
				buttTif.addActionListener(this);
				buttTif.setActionCommand("parameter");
			//}
			return buttTif;
		}
		/**
		 * This method initializes the Option:
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtJpg() {
			//if (buttJpg == null) {
				buttJpg  = new JRadioButtonMenuItem();
				buttJpg.setText("Jpg");
				buttJpg.setToolTipText("Jpg output image");
				buttJpg.addActionListener(this);
				buttJpg.setActionCommand("parameter");
			//}
			return buttJpg;
		}
		/**
		 * This method initializes the Option:	
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtPng() {
			//if (buttPng == null) {
				buttPng  = new JRadioButtonMenuItem();
				buttPng.setText("Png");
				buttPng.setToolTipText("Png output image");
				buttPng.addActionListener(this);
				buttPng.setActionCommand("parameter");
			//}
			return buttPng;
		}
		/**
		 * This method initializes the Option:	
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtBmp() {
			//if (buttBmp == null) {
				buttBmp  = new JRadioButtonMenuItem();
				buttBmp.setText("Bmp");
				buttBmp.setToolTipText("Bmp output image");
				buttBmp.addActionListener(this);
				buttBmp.setActionCommand("parameter");
			//}
			return buttBmp;
		}
		
		/**
		 * This method initializes the hidden option	
		 * 	
		 * @return javax.swing.JRadioButtonMenuItem	
		 */
		private  JRadioButtonMenuItem getJRadioButtonMenuButtExtHidden() {
			//if (buttExtHidden == null) {
				buttExtHidden = new JRadioButtonMenuItem();
				buttExtHidden.setText("Hidden");
				buttExtHidden.setVisible(false);
			//}
			return buttExtHidden;
		}


		/**
		 * This method initializes jJToolBarBar	
		 * 	
		 * @return javax.swing.JToolBar	
		 */
		private   JToolBar getJToolBarExt() {
//			if (jToolBarExt== null) {
				jToolBarExt = new JToolBar("Options");
				jToolBarExt.setOrientation(1); // 0 Horizontal 1 Vertical orientation
				jToolBarExt.add(getJRadioButtonMenuButtTif());
				jToolBarExt.add(getJRadioButtonMenuButtJpg());
				jToolBarExt.add(getJRadioButtonMenuButtPng());
				jToolBarExt.add(getJRadioButtonMenuButtBmp());
				jToolBarExt.add(getJRadioButtonMenuButtExtHidden());
				//jToolBarExt.addSeparator();
				this.setButtonGroupExt();	//Gruppieren der JRadioButtons
//			}
			return jToolBarExt;
		}

		private  void setButtonGroupExt() {
			//if (ButtonGroup buttGroup == null) {
				buttGroupExt = new ButtonGroup();
				buttGroupExt.add(buttTif);
				buttGroupExt.add(buttJpg);
				buttGroupExt.add(buttPng);
				buttGroupExt.add(buttBmp);
				buttGroupExt.add(buttExtHidden);
		}
		//------------------------------------------------------------------------------------------------
		@Override
		public void actionPerformed(ActionEvent e) {
			if (buttNN.isSelected())  interP = 0;
			if (buttBL.isSelected())  interP = 1;
			if (buttBC.isSelected())  interP = 2;
			if (buttBC2.isSelected()) interP = 3;	
		
			if (buttTif.isSelected())  extension = 0;
			if (buttJpg.isSelected())  extension = 1;
			if (buttPng.isSelected())  extension = 2;
			if (buttBmp.isSelected())  extension = 3;			
		}

		@Override
		public void stateChanged(ChangeEvent e) {
			if (jSpinnerImgNum ==  e.getSource()){
				imgNum = (((Number)jSpinnerImgNum.getValue()).intValue());
			}
			if (jSpinnerRescale ==  e.getSource()){
				rescale = (((Number)jSpinnerRescale.getValue()).intValue());
			}


//	          imgNum = (((Number)theSpinner.getValue()).intValue());
		}
	

}//END


